var ns, printModule, printModel, bluePrinter, myConnectInterval = null;
app = new Vue({
	el: '#app',
	data: {
		printId: 0,
		deviceList: [],
		form: {
			name: '',
			address: ''
		},
		list: [{
			title: '第一行标题',
			value: '第一行内容'
		}, {
			title: '第二行标题',
			value: '第二行内容'
		}],
		connect_id: '',
		connect_label: '',
		getDeviceStatus: 0, //0=未获取 1=获取失败 2=获取成功
		checkStatusCount: 0,
		lastDevice: null,
		connectCount: 1,
		issearch: false,
		printContent: ''
	},
	methods: {
		get_data: function () {
			var that = this;
			that.issearch = false;
			that.do_connect_print();
		},
		do_connect_print: function () {
			var that = this;
			var lastDevice = api.getPrefs({
				sync: true,
				key: 'last_print_device'
			});
			if (lastDevice) {
				var ret = printModel.getConnectState();
				alert(JSON.stringify(ret));
				if (ret.status && ret.state[0]) {
					that.connect_id = lastDevice;
					api.toast({
						msg: '打印机已连接'
					})
					return;
				}
				printModel.connectBleByMacAddress({
					macAddress: lastDevice
				}, function (ret, err) {
					if (ret.status) {
						if (ret.message == '已连接') {
							that.connect_id = lastDevice;
							api.toast({
								msg: '打印机已连接'
							})
						}
						else if (ret.message == '连接失败') {
							that.open_deivce();
						}
						else {
							api.toast({
								msg: '打印机连接中'
							})
						}
					}
				});
			} else {
				that.open_deivce();
			}
		},
		open_deivce() {
			var that = this;
			printModel.connectBle(function (ret, err) {
				if (ret.status) {
					if (ret.message == '已连接') {
						if (ret.data && ret.data.macAddress) {
							that.connect_id = ret.data.macAddress;
							api.setPrefs({
								key: 'last_print_device',
								value: that.connect_id
							});
						} else {
							api.toast({
								msg: '打印机连接失败'
							})
						}
					}
					else if (ret.message == '连接失败') {
						api.toast({
							msg: '打印机连接失败'
						})
					}
					else {
						api.toast({
							msg: '打印机连接中'
						})
					}
				}
			});
		},
		do_print(i) {
			var that = this;
			if (i > 1) {
				return;
			}
			printModel.printLabel({
				rows: [{
					lineType: 'size',
					width: 77,
					height: 40
				}, {
					lineType: 'gap',
					data: 0
				}, {
					lineType: 'cls'
				}, {
					lineType: 'gap',
					data: 0
				}, {//绘制文本
					lineType: 'text',
					x: 20,
					y: 16,
					rotation: 0,
					xscal: 1,
					yscal: 1,
					data: '资产名称：笔记本电脑' + i
				}, {//绘制文本
					lineType: 'text',
					x: 20,
					y: 40 + 16,
					rotation: 0,
					xscal: 1,
					yscal: 1,
					data: '资产分类：电子产品' + i
				}, {//绘制code128
					lineType: 'code128',
					x: 20,
					y: 100 + 16,
					height: 48,
					rotation: 0,
					data: '1234567890'
				}, {//绘制qrcode
					lineType: 'qrcode',
					x: 400,
					y: 16,
					level: 'L',
					cellwith: 5,
					rotation: 0,
					data: '捆包号|钢厂资源号|母卷号|品名|牌号|规格|净重|毛重'
				}]
			}, function (ret, err) {
				that.do_print(i + 1)
				// alert(JSON.stringify(ret) + "   " + JSON.stringify(err));
			});
		}
	}
});
apiready = function () {
	api.parseTapmode();
	printModel = api.require('xGprinterPlus');
	ns = window.Foresight.Util;
	app.printId = api.pageParam.printId;
	app.get_data();
};
